package com.xc.stock.utils.jackjson;


import java.io.IOException;
import java.util.Collections;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * @description: String 转 JsonList
 * @author: YUAN YONG
 * @create: 2019-07-22
 **/

public class String2JsonArrSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(
            String string, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if(StringUtils.isBlank(string)){
            jsonGenerator.writeObject(Collections.EMPTY_LIST);
        }else{
            JSONArray array = JSON.parseArray(string);
            if(array == null) {
                jsonGenerator.writeNull();
            } else {
                jsonGenerator.writeObject(array);
            }
        }

    }
}
